/*
    Shader PositionTexturedAmbientDiffuseSpecular
*/    

//Matrices
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4x4 ViewInverse : ViewInverse;

//Lumières
float4 LightPosition : Position
<
    string UIName = "Light Position";
    string Object = "PointLight";
> = {1, 1, 1, 1};

float4 AmbientColor : Ambient 
<
    string UIName = "Ambient Color";
    string UIWidget = "Color";
> = {1, 1, 1, 0.4};

float4 DiffuseColor : Diffuse
<
    string UIName = "Diffuse Color";
    string Object = "PointLight";
> = {1, 1, 1, 1};

float4 SpecularColor : Specular
<
    string UIName = "Specular Color";
    string Object = "PointLight";
> = {1, 1, 1, 1};

float SpecExpon
<
	string UIWidget = "slider";
	float UIMin = 1.0;
	float UIMax = 1024;
	float UIStep = 1.0;
	string UIName =  "Specular Power";
> = 512.0f;    

//Textures
#ifndef TEXTURED_SURFACE

Texture coloredTexture : Diffuse
<
    string ResourceType = "2D";
    string UIName = "Texture";
>;

sampler coloredTextureSampler = sampler_state{
            texture = <coloredTexture>;
            magfilter = LINEAR;
            minfilter = LINEAR;
            mipfilter = LINEAR;
            };    
#endif


//Structures de données
//Entrée dans le shader
struct AppData{
    float3 position : POSITION;
    float2 texCoordinates : TEXCOORD0;  
    
    float3 normal : NORMAL0;
};

//Sortie du vertexShader
struct VertexOutput{
    float4 position : POSITION;
    float2 texCoordinates : TEXCOORD0;
    
    float3 wP : TEXCOORD1;          //Position World       
    float3 wN : TEXCOORD2;          //Normales world    
    float3 wV : TEXCOORD3;          //View world
};


//Vertex shader
VertexOutput mainVS(AppData input){

    //Données de retour
    VertexOutput output = (VertexOutput)0;
    
    output.position = mul(float4(input.position, 1.0), WorldViewProj);
    output.texCoordinates = input.texCoordinates;
    
    output.wP = mul(input.position, World);
    output.wN = mul(input.normal, (float3x3) World);
    output.wV = ViewInverse[3].xyz - output.wP;
    
    return output;
}

float4 mainPS(VertexOutput input) : COLOR0 {
    float4 output = 0;
    
    //Calcul des variables utiles
    float3 wP = input.wP;    //Position monde du vertex 
    float3 wN = normalize(input.wN);      //Normale monde
    float3 wV = normalize(input.wV);
    float3 wL = normalize(LightPosition.xyz - wP);    //Vecteur vertex-lumière
    float3 Hn = normalize(wV + wL);  //Vecteur caméra + direction lumière
    
    //Couleur du pixel selon la texture (modifiée selon le parallax)
    float4 pixelColor = tex2D(coloredTextureSampler, input.texCoordinates);   
    
    //Calcul du vecteur lumière
    float4 lv = lit(    dot(wL, wN), 
                        dot(Hn, wN), 
                        SpecExpon);
    
    //Calcul de chaque composante
    float4 ambient = AmbientColor;
    float4 diffuse = DiffuseColor * lv.yyyy;
    float4 specular = SpecularColor * lv.zzzz;
    
    //Calcul de l'illumination finale
    //output = pixelColor * (ambient + diffuse + specular);
    output = float4(wN, 1);
    //output = float4(1.0f, 0.0f, 0.0f, 1.0f);
    //Sortie
    return output;
}

technique technique0 {
	pass p0 {
		VertexShader = compile vs_2_a mainVS();
		PixelShader = compile ps_2_a mainPS();
	}
}
